﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.UI.Web.MVP.View;
using Medianamik.UI.Web.Shop.Data;

namespace Medianamik.UI.Web.Shop
{
    public partial class ShopCategories : ModuleViewWithGrid<ICategories>, ICategories
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FilterButton.Click += OnFilterButton;
        }

        void OnFilterButton(object sender, EventArgs e)
        {
            OnFilter();
        }

        public override IMedianamikGrid Grid
        {
            get { return (IMedianamikGrid)CategoriesGrid; }
        }

        public override HyperLink AddButton
        {
            get { return AddArticleButton; }
        }

        public override IEnumerable<ListItem> Status
        {
            get { return ContentStatesList.Items.Cast<ListItem>(); }
            set
            {
                ContentStatesList.Items.AddRange(value.ToArray());
            }
        }

        public override DateTime? SelectedStartDateTime
        {
            get { return null; }
            set { }
        }

        public override DateTime? SelectedEndDateTime
        {
            get { return null; }
            set { }
        }

        public override string Keywords
        {
            get { return Name.Text; }
            set { Name.Text = value; }
        }

        public override string ListingPageUrl
        {
            get { return Pages.CategoryList; }
        }

        public override string EditPageUrl
        {
            get
            {
                return Pages.Edit;
            }
        }

        //public override Guid ParentNodeId
        //{
        //    get
        //    {

        //        return PageIds.CategoryList;
        //    }
        //}

        private Guid? _parentNodeId;
        public override Guid? ParentNodeId
        {
            get
            {
                if(_parentNodeId == null)
                {
                    _parentNodeId = GetParentNodeId();
                }

                return _parentNodeId.Value;
            }
        }
        private Guid GetParentNodeId()
        {
            var result = Request.QueryString.GetValue<Guid?>(ParamNames.ParentNodeId);

            if (result == null)
            {
                result = PageIds.CategoryList;
            }

            return result.Value;
        }

        public override string BreadcrumbTrail
        {
            get { return Breadcrumbs.Text; }
            set { Breadcrumbs.Text = value; }
        }

        public override bool IsMultiLingual
        {
            get
            {
                return true;
            }
        }

        public override Guid BaseTypeId
        {
            get
            {
                return Category.TYPE_ID;
            }
        }

        #region ParentTypeID

        private Guid? _parentTypeId;
        public Guid? ParentTypeId
        {
            get { return _parentTypeId ?? (_parentTypeId = GetParentTypeId()); }
        }
        private Guid? GetParentTypeId()
        {
            var result = Request.QueryString.GetValue<Guid?>(ParamNames.ParentType);

            if (result == null)
            {
                result = BaseTypeId;
            }

            return result;
        }

        #endregion

        public override Guid ModuleNodeId
        {
            get { return Modules.Shop; }
        }

        public override string ChildrenListingPageUrl
        {
            get
            {
                return Pages.CategoryList;
            }
        }
    }

    public interface ICategories : IModuleViewWithGrid
    {
    }
}